# Filename: jit-mda-create-snapshot.sh
# Usage: jit-mda-create-snapshot.sh <image name> <size (MB)>
# Example 1: jit-mda-create-snapshot.sh vm01 4G
# Example 2: jit-mda-create-snapshot.sh vm01 4096
#
# Obtains a LVM as parameter, creates a snapshot out of it and then
# copies the snapshot to the file /LOCALFOLDER/imagename
#
# Requires the 'pv' package.
# sudo apt-get install pv

imagename=$1
size=$2
VOLUME_GROUP=vg_datastore
LOCALFOLDER=/tmp
SNAPPATH=/dev/$VOLUME_GROUP/${imagename}-snapshot

echo `date +%H:%M:%S` Starting Script $0 $*
if [ -z "$imagename" -o -z "$size" ]; then
	echo Incorrect parameters
	echo "Usage: $0 <image name> <size (MB)>"
else
	lvcreate -L$size -s -n "${imagename}-snapshot" /dev/$VOLUME_GROUP/${imagename}-disk
	echo Copying $size bytes from $imagename disk at `date +%H:%M:%S` hours. Please wait.
	dd if=$SNAPPATH | pv -s $(blockdev --getsize64 $SNAPPATH) | dd of=$LOCALFOLDER/$imagename
fi
echo `date +%H:%M:%S` Finishing Script $0
exit 0

# To undo this script's actions:
# lvremove /dev/vg_datastore/imagename-disk-snapshot
# rm /tmp/imagename
